//
//  LabelGroupView.m
//  XSQBuies
//
//  Created by JianFeng on 16/10/12.
//  Copyright © 2016年 JianFeng. All rights reserved.
//
#define KSCEEN [UIScreen mainScreen].bounds.size
#import "LabelGroupView.h"
@interface LabelGroupView()
@property(nonatomic,strong)NSArray *labelNameArry;
//
@property(nonatomic,assign)NSInteger k;
@property(nonatomic,assign)NSInteger j;
@end
@implementation LabelGroupView
//数据的懒加载
-(NSArray *)labelNameArry
{
    if (!_labelNameArry) {
        _labelNameArry = @[@"肿瘤",@"心血管",@"神经",@"血液科",@"筋骨科",@"外科",@"妇产科"];
    }
    return _labelNameArry;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
#pragma mark - 创建控件
-(void)setupUI{
    
    self.k = 0;
    self.j = 0;
    NSInteger count = 4;
    NSInteger margin = 10;
    NSInteger Width = (KSCEEN.width - (count + 1) *margin) / count;
    NSInteger hight = 32;
    
    //文字长度分组
    NSMutableArray *rrayMThree = [[NSMutableArray alloc]init];
    NSMutableArray *rrayMFour = [[NSMutableArray alloc]init];
    for (NSString *str in self.labelNameArry) {
        if(str.length == 2)
        {
            [rrayMThree addObject:str];
        }
        if(str.length == 3)
        {
            [rrayMFour addObject:str];
        }
    }
     //for创建控件
    for (int i = 0; i < self.labelNameArry.count; i++) {
        NSInteger colIndex = i % count;
        NSInteger rowIndex = i / count;
        //创建控件
        UIButton *button = [[UIButton alloc]init];
        button.backgroundColor = [UIColor cyanColor];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
         //
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        [button becomeFirstResponder];
        
        //为button加监听
        [button addTarget:self action:@selector(cliockButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        
        CGFloat X = margin  + colIndex * (margin  + Width);
        CGFloat Y = rowIndex * (margin + hight) ;
        
        //行数的判断
        //单行
        if(rowIndex % 2 == 0){
            if(colIndex % 2 == 0)
            {    //拿3个字
                [button setTitle:rrayMThree[_j] forState:UIControlStateNormal];
                button.frame = CGRectMake( X, Y, Width -10, hight);
                _j++;
             }
            else{ //拿4个字
                [button setTitle:rrayMFour[_k] forState:UIControlStateNormal];
                button.frame = CGRectMake( X -10, Y, Width +10, hight);
                _k++;
            }
        }
        //双行
        if(rowIndex % 2 == 1){
            if(colIndex % 2 == 0)
            {    //拿4个字
                [button setTitle:rrayMFour[_k] forState:UIControlStateNormal];
                button.frame = CGRectMake( X, Y, Width +10, hight);
                _k++;
            }
            else{//拿3个字
                [button setTitle:rrayMThree[_j] forState:UIControlStateNormal];
                button.frame = CGRectMake( X + 10, Y, Width -10, hight);
                _j++;
            }
        }
    }
}

-(void)cliockButton:(UIButton *)sender
{
    if (_grunpButton) {
        _grunpButton(sender.titleLabel.text);
    }
}
 @end
